faApp.controller('MillionListCtrl',['$scope','millionService','productService','$location','$controller','$route',function($scope,millionService,productService,$location,$controller,$route) {   
  $scope.ctrlName = "MillionListCtrl";
	$controller('BaseListCtrl', {$scope: $scope}); 

  $scope.list = [];
  $scope.loading = true;

	$scope.$watch('config',function(){
    if ($scope.watchIsChange()){
      millionService.list($scope.config).then(function(json){
        $scope.list = json.list;
        $scope.pager = json.page;
        $scope.loading = false;
      });
    }
  },true);
 
  
  $scope.products = [];
  $scope.searchProduct2 = function(){
    console.log($scope.productKw);
    if ($scope.productKw){
      $scope.showSearchCombox = true;
      $scope.loaded = false;
      productService.productList({pPublicStatus:2,pName:$scope.productKw}).then(function(json){
        $scope.products = json.projectList;
        $scope.pager = json.projectDTO;
        $scope.loaded = true;
      });
    }else{
      $scope.searchMap.pId = "";
      $scope.searchMap.productKw = "";
      $scope.showSearchCombox = false;
    }
  };
  
  $scope.selectProduct = function(id,name){
    $scope.searchMap.pId = id;
    $scope.productKw = name;
    $scope.showSearchCombox = false;
  };
  
  $scope.remove = function(id){
    millionService.remove({id:id}).then(function(json){
      $route.reload();
    });
  };
}]);

faApp.controller('MillionAddCtrl',['$scope','millionService','productService','$location','$controller','$timeout', 'activityService','simuService','$route', function($scope,millionService,productService,$location,$controller,$timeout, activityService,simuService,$route) {
	$controller('MillionListCtrl', {$scope: $scope}); 

  $scope.activityCode = $location.search().activityCode || '';

  $scope.item = {};
  $scope.loading = true;
  $scope.activityList = {};
  $scope.activityListAll = {};

  // 新增，从后台查询所有活动内容
  activityService.activityNameList({isExpired: '01', isStatus: '01'}).then(function(json){
    $scope.activityList = json.list;

    $scope.item.activitiesCode = $scope.activityCode;
  });

  activityService.activityNameList().then(function(json){
    $scope.activityListAll = json.list;
  });

	$scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else{
      if ($scope.item.endTimeStr < $scope.item.beginTimeStr){
        alert("结束时间应该晚于开始时间");
        return;
      }
      if ($scope.item.beginTimeStr){$scope.item.beginTime = $scope.item.beginTimeStr + " 00:00:00";}
      if ($scope.item.endTimeStr){$scope.item.endTime = $scope.item.endTimeStr + " 23:59:59";}

      millionService.add($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.alertNextPage("操作成功");

          $route.reload();
          // $location.path("/million/list");
        }
      });
    }
  };
  
  
  $scope.products = [];
  $scope.searchProduct = function(){
    $scope.showSearchCombox = true;
    $scope.loaded = false;
    // productService.productList({pPublicStatus:2,pName:$scope.productKw,pSaleStatus:'03'}).then(function(json){
    productService.productList({pPublicStatus:2,pName:$scope.productKw}).then(function(json){
      $scope.products = json.projectList;
      $scope.pager = json.projectDTO;
      $scope.loaded = true;
    });
  };
  
  $scope.selectProduct = function(id){
    $scope.item.pId = id;
    
    angular.forEach($scope.products,function(o){
      if (o.id == id){
        
        millionService.list({pId:id,activitiesCode:$scope.item.activitiesCode}).then(function(json){
          if (json && json.list && json.list.length > 0){
            alert("此产品已经在参与活动了");
            $scope.productKw = "";
            return;
          }else{
            $scope.product = o;
            $scope.productKw = o.pName;
            
            productService.productQuery($scope.product.id).then(function(json){
              $scope.product = json.project;
              $scope.loading = false; 
              $scope.projectRabateList = json.projectRabateList;
              $scope.minRebate = 99999999;
              if ($scope.projectRabateList && $scope.projectRabateList.length > 0){
                angular.forEach($scope.projectRabateList,function(o){
                  if (o.pRabateProfit < $scope.minRebate){
                    $scope.minRebate = o.pRabateProfit;
                  }
                });
              }
            });
          }
        });
          
        $scope.showSearchCombox = false;
      }
    });
  };
  
  $scope.selectAct = function(){
    $scope.item.pId && $scope.selectProduct($scope.item.pId);
  };
 
  $scope.clickedSomewhereElse = function(){
    $scope.showSearchCombox = false;
  };

  $scope.activityProduct = {pType: '', pId: '', productName: '', activitiesCode: '', productList: {}};

  $scope.changeActivity = function(){
    $scope.activityProduct.pType = '';
    $scope.activityProduct.productList = {};
  }

  var tmpType = '';
  $scope.$watch('activityProduct.pType', function() {
    if($scope.activityProduct.pType && ($scope.activityProduct.pType == '04')){
      alert('暂不支持此类型！');
      $scope.activityProduct.pType = tmpType;
      tmpType = '';
      return;
    }

    if($scope.activityProduct.pType && (tmpType != $scope.activityProduct.pType)){
      $scope.loading = true;
      tmpType = $scope.activityProduct.pType;
      var param = {
        pageNo:1, 
        pageSize:1000,
        pTypeList: $scope.activityProduct.pType, 
        pPublicStatus:'2',
        activitiesCode: $scope.activityProduct.activitiesCode, 
      };

      // 根据类型查询对应的产品列表
      if($scope.activityProduct.pType == '06'){
        // PE/VC
        productService.queryProductList(param).then(function(json){
          $scope.activityProduct.productList = json.list;

          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }else if($scope.activityProduct.pType == '02' || $scope.activityProduct.pType == '03'){
        // 信托资管
        productService.productList(param).then(function(json){
          $scope.activityProduct.productList = json.projectList;
          
          // 删除已经添加的！
          // exclude();
          
          $scope.loading = false;
        });
      }else if($scope.activityProduct.pType == '01'){
        // 阳光私募
        simuService.list(param).then(function(json){
          $scope.activityProduct.productList = json.list;
          
          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }
    }
  }, true);

  $scope.addActivityProduct = function(){
    if ($scope.form.$invalid){
      alert('请填写完整！');
      return;
    }

    if(!$scope.activityProduct.activitiesCode){
      alert('请选择活动！');
    }else if(!$scope.activityProduct.pType){
      alert('请选择产品类型！');
    }else if(!$scope.activityProduct.pId){
      alert("请选择产品！");
    }else{
      if ($scope.activityProduct.endTimeStr < $scope.activityProduct.beginTimeStr){
        alert("结束时间应该晚于开始时间");
        return;
      }
      if ($scope.activityProduct.beginTimeStr){$scope.activityProduct.beginTime = $scope.activityProduct.beginTimeStr + " 00:00:00";}
      if ($scope.activityProduct.endTimeStr){$scope.activityProduct.endTime = $scope.activityProduct.endTimeStr + " 23:59:59";}
      


      millionService.list({pId:$scope.activityProduct.pId, activitiesCode:$scope.activityProduct.activitiesCode}).then(function(json){
        if (json && json.list && json.list.length > 0){
          alert("此产品已经在参与活动了");
          return;
        }else{
          $scope.activityProduct.productList = {};

          millionService.add($scope.activityProduct).then(function(json){
            if (json.statusCode){
              alert("出错了:" + json.statusCode);
            }else{
              $scope.activityProduct = {pType: '', pId: '', productName: '', activitiesCode: '', productList: {}};

              $scope.alertNextPage("操作成功");

              $route.reload();
              // $location.path("/million/list");
            }
          });

        }
      });

    }
  }

}]);

faApp.controller('MillionEditCtrl',['$scope','millionService','productService','$location','$controller','$routeParams','$filter','activityService','$route',function($scope,millionService,productService,$location,$controller,$routeParams,$filter,activityService,$route) {
	$controller('MillionListCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;

  $scope.activityCode = $location.search().activityCode || '';

  $scope.activityList = {};

  // 新增，从后台查询所有活动内容
  activityService.activityNameList().then(function(json){
    $scope.activityList = json.list;

    $scope.item.activitiesCode = $scope.activityCode;
  });

  millionService.list({id:$scope.id}).then(function(json){
    $scope.loading = false; 
    
    if (json.list && json.list.length > 0){
      $scope.item = json.list[0]['activitiesMollionsWealth'];
      $scope.product = json.list[0]['product'];
      if ($scope.item.beginTime){ $scope.item.beginTimeStr = $filter("date")($scope.item.beginTime,"yyyy-MM-dd"); }
      if ($scope.item.endTime){ $scope.item.endTimeStr = $filter("date")($scope.item.endTime,"yyyy-MM-dd"); }
      
      productService.productProfitList($scope.product.id).then(function(json){
        $scope.loading = false; 
        $scope.projectRabateList = json.projectRabateList;
        $scope.minRebate = 99999999;
        if ($scope.projectRabateList && $scope.projectRabateList.length > 0){
          angular.forEach($scope.projectRabateList,function(o){
            if (o.pRabateProfit < $scope.minRebate){
              $scope.minRebate = o.pRabateProfit;
            }
          });
        }
      });
    }

  });

	$scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else{
      if ($scope.item.endTimeStr < $scope.item.beginTimeStr){
        alert("结束时间应该晚于开始时间");
        return;
      }
      if ($scope.item.beginTimeStr){$scope.item.beginTime = $scope.item.beginTimeStr + " 00:00:00";}
      if ($scope.item.endTimeStr){$scope.item.endTime = $scope.item.endTimeStr + " 23:59:59";}

      millionService.edit($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.alertNextPage("操作成功");

          $route.reload();
          // $location.path("/million/list");
        }
      });
    }
  };
  
  $scope.products = [];
  $scope.searchProduct = function(){
    $scope.showSearchCombox = true;
    $scope.loaded = false;
    // productService.productList({pPublicStatus:2,pName:$scope.productKw,pSaleStatus:'03'}).then(function(json){
    productService.productList({pPublicStatus:2,pName:$scope.productKw}).then(function(json){
      $scope.products = json.projectList;
      $scope.pager = json.projectDTO;
      $scope.loaded = true;
    });
  };
  
  $scope.selectProduct = function(id){
    $scope.item.pId = id;
    
    angular.forEach($scope.products,function(o){
      if (o.id == id){
        
        millionService.list({pId:id}).then(function(json){
          if (json && json.list && json.list.length > 0){
            alert("此产品已经在参与活动了");
            $scope.productKw = "";
            return;
          }else{
            $scope.product = o;
            $scope.productKw = o.pName;
          }
        });
      }
    });
    $scope.showSearchCombox = false;
  };

  $scope.clickedSomewhereElse = function(){
    $scope.showSearchCombox = false;
  }; 

  $scope.activityProduct = {pType: '', pId: '', productName: '', activitiesCode: '', productList: {}};

  var tmpType = '';
  $scope.$watch('activityProduct.pType', function() {
    if($scope.activityProduct.pType && ($scope.activityProduct.pType == '04')){
      alert('暂不支持此类型！');
      $scope.activityProduct.pType = tmpType;
      return;
    }

    if($scope.activityProduct.pType && (tmpType != $scope.activityProduct.pType)){
      $scope.loading = true;
      tmpType = $scope.activityProduct.pType;
      var param = {
        pageNo:1, 
        pageSize:1000,
        pTypeList:$scope.activityProduct.pType, 
        pPublicStatus:'2'
      };
      // 根据类型查询对应的产品列表
      if($scope.activityProduct.pType == '06'){
        // PE/VC
        productService.queryProductList(param).then(function(json){
          $scope.activityProduct.productList = json.list;

          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }else if($scope.activityProduct.pType == '02' || $scope.activityProduct.pType == '03'){
        // 信托资管
        productService.productList(param).then(function(json){
          $scope.activityProduct.productList = json.projectList;
          
          // 删除已经添加的！
          // exclude();
          
          $scope.loading = false;
        });
      }else if($scope.activityProduct.pType == '01'){
        // 阳光私募
        simuService.list(param).then(function(json){
          $scope.activityProduct.productList = json.list;
          
          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }
    }
  }, true);

  $scope.addActivityProduct = function(){
    if ($scope.form.$invalid){
      alert('请填写完整！');
      return;
    }

    if(!$scope.activityProduct.activitiesCode){
      alert('请选择活动！');
    }else if(!$scope.activityProduct.pType){
      alert('请选择产品类型！');
    }else if(!$scope.activityProduct.pId){
      alert("请选择产品！");
    }else{
      if ($scope.activityProduct.endTimeStr < $scope.activityProduct.beginTimeStr){
        alert("结束时间应该晚于开始时间");
        return;
      }
      if ($scope.activityProduct.beginTimeStr){$scope.activityProduct.beginTime = $scope.activityProduct.beginTimeStr + " 00:00:00";}
      if ($scope.activityProduct.endTimeStr){$scope.activityProduct.endTime = $scope.activityProduct.endTimeStr + " 23:59:59";}
      
      $scope.activityProduct.productList = {};

      millionService.edit($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $scope.alertNextPage("操作成功");

          $route.reload();
          // $location.path("/million/list");
        }
      });

    }
  }

}]);

